package utils

import (
	"container/ring"
)

type IpRing struct {
	*ring.Ring
}

func NewIpRing(ips []string) *IpRing {
	if len(ips) == 0 {
		return nil
	}

	var r *ring.Ring
	for _, ip := range ips {
		next := ring.New(1)
		next.Value = ip
		if r == nil {
			r = next
		} else {
			r.Link(next)
		}
	}

	return &IpRing{
		Ring: r,
	}
}

func (i *IpRing) GetIp() string {
	if i.Ring == nil {
		return ""
	}

	value, _ := i.Value.(string)
	i.Ring = i.Next()
	return value
}
